/**
 * yeahmobi.com Inc.
 * Copyright © 2014 YeahMobi | All Rights Reserved.
 */

package com.ndp.fb.mq.service.schedulerservice.adresource;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.ndp.fb.fbagent.service.AdImageFBService;
import com.ndp.fb.model.fbagent.param.adimage.AdImageParam;
import com.ndp.fb.mongo.api.AccountAdImageMongoService;
import com.ndp.fb.mongo.model.AccountAdImageMongo;
import com.ndp.fb.mq.api.service.schedulerservice.AdAccountsSchedulerService;
import com.ndp.fb.mq.api.service.schedulerservice.AdImageSchedulerService;
import com.ndp.fb.rdb.api.EntrustAccountRdbService;
import com.ndp.fb.rdb.model.EntrustAccount;
import com.ndp.fb.util.NFSUtil;
import com.ndp.fb.util.ThreadPool;

/**
 * @author giser.yang
 * @Title: AdImageGetMd5SchedulerServiceImpl.java
 * @Copyright: Copyright © 2015
 * @Description:
 * @Company: yeahmobi
 * @Created on 下午2:39:43
 */
@Service
public class AdImageSchedulerServiceImpl implements AdImageSchedulerService {
    private final Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    NFSUtil nfsUtil;
    @Reference
    private EntrustAccountRdbService entrustAccountRdbService;
    @Autowired
    private AdAccountsSchedulerService adaccountsSchedulerService;
    @Reference
    private AdImageFBService adImageFBService;
    @Reference
    private AccountAdImageMongoService accountAdImageMongoService;

    public static void main(String[] args) {
        System.out.println("http://image.walle.spellso.com:8082/group1/M00/00/E8/rB4KeVWvsNWAQeQGAAANAcMZTfo001.jpg".replace("http://image.walle.spellso.com:8082/", ""));
    }

    /**
     * @discription
     * @author giser.yang
     * 创建时间 2015年7月17日 下午2:41:16
     */

    @Override
    public void findImageMd5() {
        ThreadPool.execute(() -> {
            try {
                logger.info("getimagemd5 start");
                List<AccountAdImageMongo> accountAdImageMongos = accountAdImageMongoService.getAccountAdImageMongosforMd5(true);
                logger.info("本次需要处理" + (accountAdImageMongos == null ? 0 : accountAdImageMongos.size()) + "张图片");
                int i = 0;
                for (AccountAdImageMongo accountAdImageMongo : accountAdImageMongos) {
                    String localUrl = nfsUtil.uploadToNFS(accountAdImageMongo.getUrl(), false);
                    String md5 = nfsUtil.getFileMd5(localUrl);
                    String localUrl128 = nfsUtil.uploadToNFS(accountAdImageMongo.getUrl128(), false);
                    accountAdImageMongoService.updateAccountAdImageUrl128MD5(accountAdImageMongo.getId(), localUrl, localUrl128, md5);
                    logger.info("本次处理第" + i++ + "张图片");

                }
                logger.info("getimagemd5 end");
            } catch (Exception e) {
                logger.error("getimagemd5 error >>>" + e.getMessage(), e);
            }
        });

    }

    /**
     * @discription
     * @author giser.yang
     * 创建时间 2015年7月17日 下午4:14:12
     * @see AdImageSchedulerService#syncAdImage()
     */

    @Override
    public void syncAdImage() {
        ThreadPool.execute(() -> {
            try {
                List<Long> entrustAccountIds = new ArrayList<>();
                List<EntrustAccount> entrustAccounts = entrustAccountRdbService.findAdAccountListByRoles("ADMIN", "GENERAL_USER", "REPORTS_ONLY");
                if (CollectionUtils.isEmpty(entrustAccounts)) {
                    logger.warn("系统没有广告账号!");
                    return;
                }
                for (EntrustAccount entrustAccount : entrustAccounts) {
                    Long fbAccountId = entrustAccount.getFbAccountId();
                    if (entrustAccountIds.contains(fbAccountId)) {
                        logger.info("账号" + fbAccountId + "账号已经被同步!");
                        continue;
                    }
                    String token = adaccountsSchedulerService.getAdaccountsToken(fbAccountId);
                    if (StringUtils.isEmpty(token)) {
                        logger.info("账号" + fbAccountId + "账号没有可用的token!");
                        continue;
                    }
                    entrustAccountIds.add(fbAccountId);
                    AdImageParam adImageParam = new AdImageParam();
                    adImageParam.setAccessToken(token);
                    adImageParam.setFbAccountId(fbAccountId);
                    adImageFBService.getAdImages(adImageParam);
                }
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        });
    }
}
